package com.onlineOrder.webAction;

import com.onlineOrder.bean.indentList;
import com.onlineOrder.bean.menuList;
import com.onlineOrder.bean.shopList;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@WebServlet({"/shopDetail","/shopMenuManage","/shopMenuUpdate","/shopBuyList","/shopMenuAdd"})
public class shopServlet extends HttpServlet {

    public static String shopID="";
    public static String shopName="";
    public static String shopPhone="";

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String servletPath = request.getServletPath();
        if (servletPath.equals("/shopDetail")){
            doShopDetail(request,response);
        }else if (servletPath.equals("/shopMenuManage")){
            doShopMenuManage(request,response);
        }else if (servletPath.equals("/shopMenuUpdate")){
            doShopMenuUpdate(request,response);
        }else if (servletPath.equals("/shopBuyList")){
            doShopBuyList(request,response);
        }else if (servletPath.equals("/shopMenuAdd")){
            doShopMenuAdd(request,response);
        }
    }

    private void doShopMenuAdd(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        String newName=request.getParameter("newName");
        String newPrice = request.getParameter("newPrice");
        String newPhone = request.getParameter("newPhone");
        String file = request.getParameter("file");

        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;

        int count=0;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/onlineOrder","root","123456");
            String sql="insert into menu (menus,img,price,shopName,shopID,shopPhone) values (?,?,?,?,?,?)";
            ps=conn.prepareStatement(sql);

            ps.setString(1,newName);
            ps.setString(2,"menu/"+file);
            ps.setString(3,newPrice);
            ps.setString(4,loginServlet.homeName);
            ps.setString(5,loginServlet.ID);
            ps.setString(6,newPhone);

            count=ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (count==1){
            request.getRequestDispatcher("/shopMenuManage").forward(request, response);
        }
    }

    private void doShopBuyList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String shopID=loginServlet.ID;

        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;

        List<indentList> buyLists = new ArrayList<>();
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/onlineOrder","root","123456");
            String sql="select menus,amount,userName,time,state from indent where shopId=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1,shopID);

            rs=ps.executeQuery();
            while (rs.next()){
                indentList indentList = new indentList();

                indentList.setMenus(rs.getString("menus"));
                indentList.setAmount(rs.getString("amount"));
                indentList.setUserName(rs.getString("userName"));
                indentList.setTime(rs.getString("time"));
                indentList.setState(rs.getString("state"));

                buyLists.add(indentList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 将一个集合放到请求域当中
        request.setAttribute("buyLists",buyLists);

        // 转发（不要重定向）
        request.getRequestDispatcher("/shopMenuBuy.jsp").forward(request, response);
    }

    private void doShopMenuUpdate(HttpServletRequest request, HttpServletResponse response) {
        String file = request.getParameter("file");
    }

    private void doShopMenuManage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String ID=loginServlet.ID;

        List<menuList> menuLists = new ArrayList<>();

        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/onlineOrder","root","123456");
            String sql="select idpic,menus,price,shopName,shopID,shopPhone from menu where shopID=?";

            ps=conn.prepareStatement(sql);
            ps.setString(1,ID);
            rs=ps.executeQuery();
            while (rs.next()){
                menuList menuList = new menuList();

                menuList.setIdPic(rs.getString("idpic"));
                menuList.setMenus(rs.getString("menus"));
                menuList.setPrice(rs.getString("price"));
                menuList.setShopName(rs.getString("shopName"));
                menuList.setShopPhone(rs.getString("shopPhone"));
                menuList.setShopID(rs.getString("shopID"));

                menuLists.add(menuList);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        // 将一个集合放到请求域当中
        request.setAttribute("shopMenuManage",menuLists);

        // 转发（不要重定向）
        request.getRequestDispatcher("/shopMenuManage.jsp").forward(request, response);

    }

    private void doShopDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String shopName=loginServlet.homeName;
        shopList shopList = new shopList();

        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/onlineOrder","root","123456");
            String sql="select id,shopName,sale,Pwd,name,phone,message,address,state,register,flag from shop where shopName=?";
            ps=conn.prepareStatement(sql);
            ps.setString(1,shopName);
            rs=ps.executeQuery();

            if (rs.next()){
                shopList.setShopId(rs.getString("id"));
                shopList.setShopName(rs.getString("shopName"));
                shopList.setSale(rs.getString("sale"));
                shopList.setPassword(rs.getString("Pwd"));
                shopList.setOwner(rs.getString("name"));
                shopList.setPhone(rs.getString("phone"));
                shopList.setMessage(rs.getString("message"));
                shopList.setAddress(rs.getString("address"));
                shopList.setState(rs.getString("state"));
                shopList.setRegister(rs.getString("register"));
                shopList.setFlag(rs.getString("flag"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //         将一个集合放到请求域当中
        request.setAttribute("shopList",shopList);

//         转发（不要重定向）
        request.getRequestDispatcher("/shopDetail.jsp").forward(request, response);
    }
}
